package java_option.audio;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

import java_option.audio.config.AudioConfig;

/**
 * 
 * @author java红了 | java_option <br>
 *         2020-11-49:04:56
 */
public class AudioRecorder extends Thread {

	private Boolean isRunning;

	private TargetDataLine targetDataLine;
	private byte[] audioBytes;

	private int sampleRate = AudioConfig.sampleRate;
	private boolean isBigEndian = AudioConfig.isBigEndian;
	private int sampleSizeInBits = AudioConfig.sampleSizeInBits;
	private int channels = AudioConfig.channels;

	public AudioRecorder() {
		/**
		 * 注意
		 */
		AudioFormat audioFormat = new AudioFormat(sampleRate, sampleSizeInBits, channels, AudioConfig.isSigned, isBigEndian);
//

		int sampleRate = (int) audioFormat.getSampleRate();
		int numChannels = audioFormat.getChannels();

		int audioBufferSize = sampleRate * numChannels;
		audioBytes = new byte[audioBufferSize];

		DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
		try {
			targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
//
//
//
			targetDataLine.open(audioFormat, audioBufferSize);
		} catch (LineUnavailableException e1) {
			e1.printStackTrace();
		}
	}

	public void onStart() {
		targetDataLine.start(); //
		isRunning = true;
		this.start();//
		synchronized (this) {
			this.notify();
		}
	}

	public void onStop() {
		isRunning = null;
		targetDataLine.stop();
		targetDataLine.close();
	}

//
	public void onPause() {
		targetDataLine.stop();
		this.isRunning = false;
	}

	public void onResume() {
		targetDataLine.start();
		this.isRunning = true;
		synchronized (this) {
			this.notify();
		}
	}

	public void run() {
		SpeechStore ss = new SpeechStore();
		SpeechStore.init();
		while (true) {
			if (isRunning == null) {
				System.out.println("停止");
				break;
			}
			if (isRunning == false) {
				synchronized (this) {
					try {
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}

			int nBytesRead = targetDataLine.read(audioBytes, 0, targetDataLine.available());
			byte[] data = Arrays.copyOf(audioBytes, nBytesRead);

//
//
			if (data.length > 0) {
				System.out.println("正在往文件写入数据，dataSize " + data.length);
				ss.writeToWaveFile(data, true, isBigEndian);
			}
		}
	}

	public static void main(String[] args) {
		File file = new File("D:/AudioRecorder1603786503506.wav");
		try {
			RandomAccessFile raf = new RandomAccessFile(file, "rw");

			byte[] data = null;

//
			data = new byte[4];
			raf.read(data);
			System.out.println(new String(data));

			data = new byte[4];
			raf.read(data);
			int length = bytesToInt(data);
			System.out.println(length);

			data = new byte[4];
			raf.read(data);
			System.out.println(new String(data));

//

			raf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	static void print(byte[] data) {
		System.out.print(data[0] + " " + data[1] + " " + data[2] + " " + data[3]);
		System.out.println();
	}

	/**
	 * 将byte数组转换为整数
	 * 
	 * @param bs
	 * @return
	 */
	public static int bytesToInt(byte[] bs) {
		int a = 0;
		for (int i = bs.length - 1; i >= 0; i--) {
			a += bs[i] * Math.pow(255, bs.length - i - 1);
		}
		return a;
	}
}
